import tkinter as tk
import tkinter.messagebox
import requests
import os
from face_detection import face_detection
from fire_detection import fire_detection
from circle_detection import circle_detection
from PIL import ImageTk, Image


def getManyPages(keyword, pages):
    params = []
    for i in range(30, 30 * pages + 30, 30):
        params.append({
            'tn': 'resultjson_com',
            'ipn': 'rj',
            'ct': 201326592,
            'is': '',
            'fp': 'result',
            'queryWord': keyword,
            'cl': 2,
            'lm': -1,
            'ie': 'utf-8',
            'oe': 'utf-8',
            'adpicid': '',
            'st': -1,
            'z': '',
            'ic': 0,
            'word': keyword,
            's': '',
            'se': '',
            'tab': '',
            'width': '',
            'height': '',
            'face': 0,
            'istype': 2,
            'qc': '',
            'nc': 1,
            'fr': '',
            'pn': i,
            'rn': 30,
            'gsm': '1e',
            '1488942260214': ''
        })
    url = 'https://image.baidu.com/search/acjson'
    urls = []
    for i in params:
        try:
         urls.append(requests.get(url, params=i).json().get('data'))
        except ReadTimeout:
         print(timeout)


    return urls


def getImg(dataList, localPath):

    if not os.path.exists(localPath):  # 新建文件夹
        os.mkdir(localPath)

    x = 1
    for alist in dataList:
        for i in alist:
            if i.get('middleURL') != None:
                print('正在下载：%s' % i.get('thumbURL'))
                ir = requests.get(i.get('middleURL'))
                open(localPath + '%d.jpg' % x, 'wb').write(ir.content)
                x += 1
            else:
                #print(tk.messagebox.showinfo(title='提示', message='下载完成'))
                pass
    print(tk.messagebox.showinfo(title='提示', message='下载完成'))




if __name__ == '__main__':

    def do_it():
        
        var_keyword = entry_keyword.get()
        var_page = entry_page.get()
        var_file_name = entry_file_name.get()
        
       
        if (var_keyword == '' or var_page == '' or var_file_name == ''):
            print(tk.messagebox.showerror(title='异常', message='输入不能为空'))
        else:
            dataLists = getManyPages(var_keyword, int(var_page))
            getImg(dataLists, 'e:/work/%s/' % var_file_name)
            
            
    def detect1():
        
        var_file1_name=entry_file1_name.get()
        var_file2_name=entry_file2_name.get()
        if (var_file1_name == ''or var_file2_name == ''):
            print(tk.messagebox.showerror(title='异常', message='输入不能为空'))
        else:
            face_detection('e:\\work\\'+var_file1_name+'\\','e:\\work\\'+var_file2_name+'\\')
    def detect2():
        
        var_file1_name=entry_file1_name.get()
        var_file2_name=entry_file2_name.get()
        if (var_file1_name == ''or var_file2_name == ''):
            print(tk.messagebox.showerror(title='异常', message='输入不能为空'))
        else:
            fire_detection('e:\\work\\'+var_file1_name+'\\','e:\\work\\'+var_file2_name+'\\')
    def detect3():
        
        var_file1_name=entry_file1_name.get()
        var_file2_name=entry_file2_name.get()
        if (var_file1_name == ''or var_file2_name == ''):
            print(tk.messagebox.showerror(title='异常', message='输入不能为空'))
        else:
            circle_detection('e:\\work\\'+var_file1_name+'\\','e:\\work\\'+var_file2_name+'\\')
            


    def open_help():
        window_help = tk.Toplevel(window)
        window_help.geometry('500x200')
        window_help.title('帮助')
        
        canvas1 = tk.Canvas(window_help, height=1000, width=1800,bd=0, highlightthickness=0)
        imgpath = 'yan.gif'
        img = Image.open(imgpath)
        photo = ImageTk.PhotoImage(img)
        canvas1.create_image(0,100,anchor='nw',image=photo)
        canvas1.pack()

        tk.Label(window_help, text='关键字: 输入你想搜索的关键字', font=('Arial',10)).place(x=30, y=35)
        tk.Label(window_help, text='页数：默认每页30张图片', font=('Arial',10)).place(x=30, y=75)
        tk.Label(window_help, text='文件名：输入存储的文件名，存在则直接存储，不存在创建后存储', font=('Arial',10)).place(x=30, y=115)
    

    window = tk.Tk()
    window.attributes("-alpha", 0.4)
    window.title('图片搜索下载')
    window.geometry('500x500')
    window.attributes("-alpha", 0.95)




    # 欢迎图片
    canvas = tk.Canvas(window, height=1000, width=1800,bd=0, highlightthickness=0)
    imgpath = 'yan.gif'
    img = Image.open(imgpath)
    photo = ImageTk.PhotoImage(img)
    canvas.create_image(0,0,anchor='nw',image=photo)
    canvas.create_rectangle(20, 60, 250, 360,
    outline='gray', # 边框颜
    width=2 # 边框宽度
    )
    canvas.create_rectangle(270, 120, 480, 390,
    outline='gray', # 边框颜色
    width=2 # 边框宽度
    )
    canvas.pack()

    tk.Label(window, text='关键字: ', bg='gray').place(x=30, y=100)
    tk.Label(window, text='下载页数: ', bg='gray').place(x=30, y=190)
    tk.Label(window, text='文件名: ', bg='gray').place(x=30, y=280)
    tk.Label(window, text='原文件: ', bg='gray').place(x=280, y=150)
    tk.Label(window, text='新文件: ', bg='gray').place(x=280, y=250)
    

    # var_keyword.set('在这里输入关键字')
    # entry_keyword = tk.Entry(window, textvariable=var_keyword)
    entry_keyword = tk.Entry(window,bg='gray')
    entry_keyword.place(x=90, y=100)

    # var_page.set('1')
    # entry_page = tk.Entry(window, textvariable=var_page)
    entry_page = tk.Entry(window,bg='gray')
    entry_page.place(x=90, y=190)

    # var_file_name.set('img')
    # entry_file_name = tk.Entry(window, textvariable=var_file_name)
    entry_file_name = tk.Entry(window,bg='gray')
    entry_file_name.place(x=90, y=280)

    entry_file1_name = tk.Entry(window,bg='gray')
    entry_file1_name.place(x=330, y=150)

    entry_file2_name = tk.Entry(window,bg='gray')
    entry_file2_name.place(x=330, y=250)
    


    btn_login = tk.Button(window, text='点击搜索', command=do_it,bg='gray')
    btn_login.place(x=110, y=320)

    btn_login = tk.Button(window, text='识别人脸', command=detect1,bg='gray')
    btn_login.place(x=350, y=285)
    
    btn_login = tk.Button(window, text='识别火焰', command=detect2,bg='gray')
    btn_login.place(x=350, y=320)
    btn_login = tk.Button(window, text='识别圆形', command=detect3,bg='gray')
    btn_login.place(x=350, y=355)
    btn_login = tk.Button(window, text='说明', command=open_help,bg='gray')
    btn_login.place(x=240, y=420)

    

    window.mainloop()


